#!/bin/bash

echo "install running"

prefixDir="/usr/local"
installDir="${prefixDir}/ped"
binInstallDir="${prefixDir}/ped/bin"
dataDir="${installDir}/data"
logDir="${installDir}/log"
confDir="${installDir}/conf"

ldconfigFile="/etc/ld.so.conf.d/ped.conf"
serviceFile="/etc/systemd/system/ped.service"
upgradeConfDir="${upgradeInstallDir}/conf"
thirdPartyDir=${installDir}/third_party

chmod 777 ./uninstall.sh
./uninstall.sh


rootPath=$(pwd)

createDir(){
	if [ ! -d ${1} ];then
		mkdir -p ${1}
	fi
}

copyFile(){
	if [ -f ${1} ];then
		cp -rfp ${1} ${2}
	fi
}

createDir ${installDir}
createDir ${dataDir}
createDir ${logDir}
createDir ${confDir}
createDir ${binInstallDir}
createDir ${thirdPartyDir}



echo "install ped"
cp -rfp ./ped ${prefixDir}



cd ${installDir}/bin
chmod 777 *

chmod 644 ${confDir}/settings.conf

cd ${installDir}/lib
chmod 644 *

if [ -f libuv.so.1.0.0 ];then
	ln -sf libuv.so.1.0.0 libuv.so.1
	ln -sf libuv.so.1 libuv.so
fi
mv libssl.so libssl.so.1.1
ln -sf libssl.so.1.1 libssl.so.1
ln -sf libssl.so.1 libssl.so

cd ${installDir}/module


cd ${rootPath}
chmod 644 -R ./etc/*
cp -rfp ./etc/* /etc/

ldconfig

cd ${rootPath}

echo "running service"
if [ -f ${binInstallDir}/ped ];then
	systemctl enable ped.service --now
	systemctl daemon-reload
	systemctl start ped
fi
if [ -f ${binInstallDir}/upgrade ];then
	systemctl enable upgrade.service --now
	systemctl daemon-reload
	systemctl start ped
fi

echo "protocol engine install finished"


